package com.e3.sysmg.config.client;

import java.util.List;

import com.e3.sysmg.config.model.ParameterConstant;
import com.e3.sysmg.config.model.ParameterModel;
import com.e3.sysmg.config.model.ParameterTreeModel;
import com.e3.template.tgf.TGFTreePanelAsync;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class ParameterTreePanel extends TGFTreePanelAsync<ParameterTreeModel> {
	
	private ParameterRPCServiceAsync service = ParameterRPCService.Util.getInstance();

	@Override
	public String getTreeDisplayFieldName() {
		return ParameterTreeModel.NAME;
	}

	@Override
	protected boolean isRootModel(ParameterTreeModel model) {
		if(model.getCode()==0){
			return true;
		}
		return false;
	}

	@Override
	public void excuteLoadModelChildren(ParameterTreeModel pModel,AsyncCallback<List<ParameterTreeModel>> callback) {
		service.getParameterTypes(pModel, callback);
	}

	@Override
	public String getRootDisplayName() {
		return ParameterMessages.INSTANCE.tree_root_name();
	}

	@Override
	protected void onTreeLoadSuccess(ParameterTreeModel loadConfig,	List<ParameterTreeModel> result) {
		super.onTreeLoadSuccess(loadConfig, result);
		if(result!=null){
			for (ParameterTreeModel parameterTreeModel : result) {
				//parameterTreeModel.setParentModel(loadConfig);
				Integer code = parameterTreeModel.getCode();
				String name = null;
				if(code == ParameterConstant.TYPE_COMMON){
					name =ParameterModel.TYPE_COMMON;
				}
				else if(code == ParameterConstant.TYPE_PURCHASE){
					name = ParameterModel.TYPE_PURCHASE;
				}
				else if(code == ParameterConstant.TYPE_SALES){
					name = ParameterModel.TYPE_SALES;
				}
				else if(code == ParameterConstant.TYPE_INVENTORY){
					name = ParameterModel.TYPE_INVENTORY;
				}
				else if(code == ParameterConstant.TYPE_ARAP){
					name = ParameterModel.TYPE_ARAP;
				}
				
				if(name!=null ){
					parameterTreeModel.setName( getFieldDisplayName(name));
				}
				
			}
		}
	}

	private String getFieldDisplayName(String name){
		return ParameterHelper.INSTANCE.getFieldDisplayName(name);
	}


}
